package com.en.effectiveJava.chapter2.Builder2;

/**
 * 营养成分模型
 *  用 Builder 构造
 *  一旦创建不再改变
 * Create by EN
 * On 24/05/18 12:39 PM
*/
public class NutritionFacts {

    /**  食物大小   */
    private final int servingSize;

    /**  食物编号   */
    private final int servings;

    /**  热量/卡路里   */
    private final int calories;

    /**  脂肪含量   */
    private final int fat;

    /**  钠含量   */
    private final int sodium;

    /**  碳水化合物含量   */
    private final int carbohydrate;

    /**
     * 构建 NutritionFacts 的 Builder 类
     */
    public static class Builder {
        // 必要属性，需要参数传递
        private final int servingSize;
        private final int servings;

        // 非必要属性，赋予默认值
        private int calories      = 0;
        private int fat           = 0;
        private int sodium        = 0;
        private int carbohydrate  = 0;

        public Builder(int servingSize, int servings) {
            this.servingSize = servingSize;
            this.servings = servings;
        }

        public Builder calories(int val) {
            calories = val;
            return this;
        }

        public Builder fat(int val) {
            fat = val;
            return this;
        }

        public Builder sodium(int val) {
            sodium = val;
            return this;
        }

        public Builder carbohydrate(int val) {
            carbohydrate = val;
            return this;
        }

        public NutritionFacts build() {
            return new NutritionFacts(this);
        }
    }

    private NutritionFacts(Builder builder) {
        servingSize = builder.servingSize;
        servings = builder.servings;
        calories = builder.calories;
        fat = builder.fat;
        sodium = builder.sodium;
        carbohydrate = builder.carbohydrate;
    }

    public int getServingSize() {
        return servingSize;
    }

    public int getServings() {
        return servings;
    }

    public int getCalories() {
        return calories;
    }

    public int getFat() {
        return fat;
    }

    public int getSodium() {
        return sodium;
    }

    public int getCarbohydrate() {
        return carbohydrate;
    }

    @Override
    public String toString() {
        return "NutritionFacts{" +
                "servingSize=" + servingSize +
                ", servings=" + servings +
                ", calories=" + calories +
                ", fat=" + fat +
                ", sodium=" + sodium +
                ", carbohydrate=" + carbohydrate +
                '}';
    }
}
